﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kunlun.ViewModels;
using Kunlun.Models;
using System.Web;
namespace Kunlun.BusinessLogic
{
    public static class KunlunCarts
    {
        private static decimal _allprice = (decimal)0;
        private static string _product = string.Empty;
        public static decimal Allprice { get { return _allprice; } }
        public static string Product { get { return _product; } }

        public static List<UserCartViewModels> DeleteCart(int index)
        {
            List<UserCartViewModels> list = GetCars(true);
            UserCartViewModels model = list.FirstOrDefault(p => p.Index == index);
            while (model != null)
            {
                list.Remove(model);
                model = list.FirstOrDefault(p => p.Index == index);
            }
            HttpCookie cookie = KunlunCookies.GetCarCookis();
            cookie.Value = string.Empty;

            foreach (UserCartViewModels m in list)
            {
                cookie.Value += "|" + m.PID + "," + m.Attributes + "," + m.Index + "," + m.Types + "," + m.Level + "," + m.Size + "," + m.Numbers + "," + m.WorkID;
            }
            KunlunCookies.ResetCookis(cookie);
            list = GetCars(true);
            return list;
        }

        public static List<UserCartViewModels> ModifyCart(int id, int number)
        {
            List<UserCartViewModels> list = GetCars(true);

            HttpCookie cookie = KunlunCookies.GetCarCookis();
            cookie.Value = string.Empty;
            foreach (UserCartViewModels m in list)
            {
                if (m.Index == id)
                {
                    cookie.Value += "|" + m.PID + "," + m.Attributes + "," + m.Index + "," + m.Types + "," + m.Level + "," + m.Size + "," + number + "," + m.WorkID;
                }
                else
                {
                    cookie.Value += "|" + m.PID + "," + m.Attributes + "," + m.Index + "," + m.Types + "," + m.Level + "," + m.Size + "," + m.Numbers + "," + m.WorkID;
                }
            }
            KunlunCookies.ResetCookis(cookie);
            list = GetCars(true);
            return list;
        }

        /// <summary>
        /// 添加购物车cookie
        /// </summary>
        /// <param name="ProductID">产品ID</param>
        /// <param name="AttrID">规格ID</param>
        /// <param name="ft">框画类型</param>
        /// <param name="level">卡纸层数</param>
        /// <param name="size">尺寸</param>
        /// <param name="index">第几个产品</param>
        /// <param name="WorkID">作品ID</param>
        /// <param name="number">购买数量</param>
        public static bool InsertCart(string ProductID, int AttrID, FrameType ft, PaperLevel level, PaperSize size, int index, int WorkID, int number)
        {
            List<UserCartViewModels> cars = GetCars(false);
            if (cars.Count(o => o.PID == ProductID && o.Index == index) > 0)
            {
                return false;
            }
            if (cars.Count(o => o.PID == ProductID && o.WorkID == WorkID) > 0)
            {
               if(cars.Count(p=>p.Types==(int)ft)>0)
               {
                   return false;
               }  
            }
            HttpCookie cookie = KunlunCookies.GetCarCookis();
            cookie.Value += "|" + ProductID + "," + AttrID + "," + index + "," + (int)ft + "," + (int)level + "," + (int)size + "," + number + "," + WorkID;
            KunlunCookies.ResetCookis(cookie);
            return true;
        }

        /// <summary>
        /// 获取当前客户的所有购物车中的产品实体集合
        /// </summary>
        /// <param name="isAll">是否读取全部</param>
        /// <returns>返回客户购物车显示实体集合</returns>
        public static List<UserCartViewModels> GetCars(bool isAll)
        {
            _allprice = 0;
            var cars = new List<UserCartViewModels>();              //实例化客户购物车显示实体
            HttpCookie cookie = KunlunCookies.GetCarCookis();
            string[] items = cookie.Value.Split('|');               //客户cookie键值 数组
            foreach (string str in items)
            {
                if (string.IsNullOrEmpty(str)) continue;            //如果当前循环的str为空  继续下一个

                string[] attrs = str.Split(',');
                if (attrs.Length < 7) continue;

                var car = new UserCartViewModels();
                car.PID = attrs[0];
                car.Attributes = Convert.ToInt32(attrs[1]);
                car.Index = Convert.ToInt32(attrs[2]);
                car.Types = Convert.ToInt32(attrs[3]);
                car.Level = Convert.ToInt32(attrs[4]);
                car.Size = Convert.ToInt32(attrs[5]);
                car.Numbers = Convert.ToInt32(attrs[6]);
                car.WorkID = Convert.ToInt32(attrs[7]);

                Product p = KunlunProduct.GetProductModel(car.PID);
                if (p == null) continue;                                        //执行下一个循环

                if (isAll)
                {
                    //规格实体
                    Product_attributes attr = KunlunProductAttribute.GetProductAttributes(car.Attributes);
                    if (attr != null)
                    {
                        if (car.Types == 2)
                            car.Prices = attr.Value2;
                        else
                            car.Prices = attr.Value1;

                        car.Propertys = attr.Title;         //属性名称
                    }
                    else if (car.WorkID != 0)
                    {
                        Works w = new FlexSave().GetWork(car.WorkID);
                        if (w == null) continue;                                //执行下一个循环

                        car.Prices = p.Kacaprice * w.Height * w.Width / 10000;
                    }
                }

                car.Thumnail = ProductImageToThum.Thumbnail(p.ViewPic);
                car.Product = p.Name;
                if (car.Types == 0)
                {
                    UserCartViewModels mcar = cars.FirstOrDefault(o => o.Types != 0);
                    Product_attributes attr = KunlunProductAttribute.GetProductAttributes(mcar.Attributes);
                    int width = 0;
                    int height = 0;
                    if (attr != null)
                    {
                        width = attr.Width;
                        height = attr.Height;
                    }
                    else if (mcar.WorkID != 0)
                    {
                        Works w = new FlexSave().GetWork(car.WorkID);
                        width = w.Width;
                        height = w.Height;
                    }
                    switch (car.Level)
                    {
                        case 0:
                            car.Prices = width * height * p.Kacaprice / 10000;
                            break;
                        case 4:
                            car.Prices = (width + height) * p.Kacaprice / 50;
                            break;
                        default:
                            car.Prices = width * height * p.Kacaprice / 10000;
                            break;

                    }
                }
                _allprice += car.Prices * car.Numbers;
                cars.Add(car);
            }
            return cars;
        }

        public static int GetCarCount()
        {
            List<UserCartViewModels> cars = GetCars(false);
            if (cars.Count == 0)
                return 0;

            return cars.Select(o => o.Index).Distinct().Count();
        }

        public static int MaxItems()
        {
            List<UserCartViewModels> cars = GetCars(false);
            if (cars.Count == 0)
            {
                return 1;
            }
            return cars.Max(o => o.Index) + 1;
        }

        /// <summary>
        /// 清空购物车的cookie
        /// </summary>
        public static void ClearCar()
        {
            KunlunCookies.ClearCarCookis();
        }
    }

    /// <summary>
    /// 卡纸层数
    /// </summary>
    public enum PaperLevel
    {
        无 = 0,
        一层 = 1,
        二层 = 2,
        三层 = 3,
        外框 = 4
    }

    /// <summary>
    /// 卡纸尺寸
    /// </summary>
    public enum PaperSize
    {
        小 = 0,
        中 = 1,
        大 = 2
    }

    /// <summary>
    /// 框画类型
    /// </summary>
    public enum FrameType
    {
        无框画 = 1,
        海报 = 2,
        框画 = 3,
        水晶画 = 4
    }
}
